package com.hx.setting.action.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.StrutsStatics;

/**
 * Generate Date : 2014-03-16
 * Generate Version : v3.0
 * Generate Name : Honey v3.1 Test
 * Generate User : THINK
 * JDK Version : 1.5
 */
@SuppressWarnings("serial")
public class RequestDumpInterceptor extends AbstractInterceptor
{

	/** 是否打印parameter信息,默认是 */
	private boolean	parameter	= true;

	/** 是否打印header信息,默认否 */
	private boolean	header		= false;

	/** 是否打印attribute信息,默认否 */
	private boolean	attribute	= false;

	/** 是否打印action信息,默认否 */
	private boolean	action		= false;

	/** 是否打印stack信息,默认否 */
	private boolean	stack		= false;

	public boolean isParameter()
	{
		return parameter;
	}

	public void setParameter(boolean parameter)
	{
		this.parameter = parameter;
	}

	public boolean isHeader()
	{
		return header;
	}

	public void setHeader(boolean header)
	{
		this.header = header;
	}

	public boolean isAction()
	{
		return action;
	}

	public void setAction(boolean action)
	{
		this.action = action;
	}

	public boolean isAttribute()
	{
		return attribute;
	}

	public void setAttribute(boolean attribute)
	{
		this.attribute = attribute;
	}

	public boolean isStack()
	{
		return stack;
	}

	public void setStack(boolean stack)
	{
		this.stack = stack;
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception
	{
		ActionContext actionContext = invocation.getInvocationContext();
		HttpServletRequest request = (HttpServletRequest) actionContext.get( StrutsStatics.HTTP_REQUEST );
		if ( parameter )
		{
			Enumeration<String> e = request.getParameterNames();
			while ( e.hasMoreElements() )
			{
				String key = e.nextElement();
				dempMessage( "Parameter" ,key ,request.getParameter( key ) );
			}
		}
		if ( attribute )
		{
			Enumeration<String> e = request.getAttributeNames();
			while ( e.hasMoreElements() )
			{
				String key = e.nextElement();
				dempMessage( "Attribute" ,key ,request.getAttribute( key ) );
			}
		}
		if ( header )
		{
			Enumeration<String> e = request.getHeaderNames();
			while ( e.hasMoreElements() )
			{
				String key = e.nextElement();
				dempMessage( "Header" ,key ,request.getHeader( key ) );
			}
		}
		if ( action )
		{
			ActionProxy ap = invocation.getProxy();
			dempMessage( "Action" ,"Namespace" ,ap.getNamespace() );
			dempMessage( "Action" ,"Method" ,ap.getMethod() );
			dempMessage( "Action" ,"ActionName" ,ap.getActionName() );
			dempMessage( "Action" ,"ActionClass" ,ap.getAction().getClass().getName() );
		}
		if ( stack )
		{
			Map<String, Object> o = invocation.getStack().getContext();
			for ( Map.Entry<String, Object> en : o.entrySet() )
			{
				dempMessage( "Stack" ,en.getKey() ,en.getValue() );
			}
		}
		return invocation.invoke();
	}

	private final void dempMessage(String dempName, String key, Object message)
	{
		System.out.println( dempName + " ==> " + key + "  :  " + message.toString() );
	}
}
/* Honey v3.1 Test - version:1 - OriginalChecksum:B3b836c3967f3e615c67a2e395b13eb66 (do not edit this line) */
